package com.example.musicplayer

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MusicPlayer:Fragment() {

    private val musicData=ArrayList<music>()  //存放music list数据的集合
    private lateinit var mediaPlayer: MediaPlayer
    private lateinit var spinImage:ImageView
    private lateinit var StartOrPauseBtn:ImageButton
    private lateinit var stopBtn:ImageButton

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.musicplayer,container,false)
        spinImage=view.findViewById(R.id.spinImage)
        StartOrPauseBtn=view.findViewById(R.id.StartOrPauseBtn)
        stopBtn=view.findViewById(R.id.stopBtn)
        mediaPlayer= MediaPlayer()
        return view
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //制作音乐列表,网络请求包括解析数据获取数据
        val JSONWithMusicListService=ServiceCreator.create(JSONImageService::class.java)
        JSONWithMusicListService.getListData().enqueue(object : Callback<List<music>> {
            override fun onResponse(call: Call<List<music>>, response: Response<List<music>>) {
                val list=response.body()
                if (list!=null) {
                    for (i in list) {
                        musicData.add(i)
                    }
                }
                Glide.with(this@MusicPlayer)
                    .load(musicData[0].coverImgUrl)
                    .apply(RequestOptions.circleCropTransform())
                    .into(spinImage)
                StartOrPauseBtn.setImageResource(R.drawable.play)
                stopBtn.setImageResource(R.drawable.replay)
                initMediaPlayer(musicData[0].src)
                setupStartOrPauseClickListener()
                setupStopClickListener(musicData[0].src)
            }
            override fun onFailure(p0: Call<List<music>>, p1: Throwable) {
                TODO("Not yet implemented")
            }
        })
    }
    //音乐播放器资源释放和初始化
    private fun initMediaPlayer(musicUrl:String) {
        //释放之前的MediaPlayer资源
        mediaPlayer.release()
        //重新初始化MediaPlayer
        mediaPlayer=MediaPlayer.create(requireContext(), Uri.parse(musicUrl))
        //返回最初按钮状态
        StartOrPauseBtn.setImageResource(R.drawable.play)
    }
    //播放和停止按钮
    private fun setupStartOrPauseClickListener() {
        StartOrPauseBtn.setOnClickListener {
            if (!mediaPlayer.isPlaying) {
                mediaPlayer.start()
                StartOrPauseBtn.setImageResource(R.drawable.pause)
            }
            else {
                mediaPlayer.pause()
                StartOrPauseBtn.setImageResource(R.drawable.play)
            }
        }
    }
    //停止按钮
    private fun setupStopClickListener(musicUrl:String) {
        stopBtn.setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
            else if (!mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
        }
    }

}